package com.example.im.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.widget.Toast;

import androidx.annotation.Nullable;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.example.common.base.AbsActivity;
import com.example.common.utils.SPUtils;
import com.example.im.R;
import com.example.im.databinding.ActLoginBinding;
import com.example.im.utils.MessageUtils;
import com.example.im.utils.RouterUtils;

import static com.example.common.utils.SPUtils.IM_LOGIN;
import static com.example.common.utils.SPUtils.IM_PHOTO;

@Route(path = "/im/login")
public class LoginActivity extends AbsActivity<ActLoginBinding> {

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    protected int getLayoutId() {
        return R.layout.act_login;
    }

    @Override
    protected void main() {

    }

    @Override
    protected void initListener() {
        mBinding.btnSubmit.setOnClickListener(v -> {
            if (check()) {
                String uid = mBinding.etLoginName.getText().toString().trim();
                MessageUtils.getInstance().login(uid, data -> {
                    if (data) {
                        SPUtils.getInstance().setStringValue(IM_LOGIN, mBinding.etLoginName.getText().toString().trim());

                        MessageUtils.getInstance().setOfflinePush(14774, uid, data1 -> getUserInfo());
                    }
                });
            }
        });

    }

    private void getUserInfo() {
        MessageUtils.getInstance().getUserInfo(SPUtils.getInstance().getStringValue(IM_LOGIN), data -> {
            SPUtils.getInstance().setStringValue(IM_PHOTO, data.get(0).getFaceUrl());
            RouterUtils.jumpImMain(mContext);
            finish();
        });
    }

    private boolean check() {
        if (TextUtils.isEmpty(mBinding.etLoginName.getText().toString().trim())) {
            Toast.makeText(mContext, "请输入登录账号", Toast.LENGTH_SHORT).show();
            return false;
        }
        return true;
    }
}
